package com.fdb.efp.console.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.service.facade.SequenceService;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.efp.console.service.facade.AccLoanService;
import com.fdb.efp.console.service.vo.AccLoanVO;
import com.fdb.efp.console.service.facade.ColltTaskInfoService;
import com.fdb.efp.console.service.facade.ColltWriteOffAppService;
import com.fdb.efp.console.service.vo.ColltTaskInfoVO;
import com.fdb.efp.console.service.vo.ColltWriteOffAppVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api")
public class ColltWriteOffAppController extends AbstractController  {

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	@Qualifier("colltWriteOffAppService")
	private ColltWriteOffAppService colltWriteOffAppService;

	@Autowired
	@Qualifier("colltTaskInfoService")
	private ColltTaskInfoService colltTaskInfoService;

	@Autowired
	@Qualifier("dbSeqService")
	private SequenceService sequenceService;

	@Autowired
	@Qualifier("accLoanService")
	private AccLoanService accLoanService;

	/**
	 * 查询列表数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2020/3/16 14:50
	 * @version 0.1
	 */
	@RequestMapping(value="/collt/write/off/apps/biz",method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ColltWriteOffAppVO>> queryColltWriteOffAppByBiz(@RequestBody ColltWriteOffAppVO vo) {
		List<ColltWriteOffAppVO> outputVo = colltWriteOffAppService.queryByPk(vo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 查询列表数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2020/3/10 14:50
	 * @version 0.1
	 */
	@RequestMapping(value="/collt/write/off/apps",method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ColltWriteOffAppVO>> queryColltWriteOffApp(@RequestBody ColltWriteOffAppVO vo) {
		Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		List<ColltWriteOffAppVO> outputVo = null;
		switch (dataRuleType) {
			case Owner:
				outputVo = colltWriteOffAppService.queryAllOwner(vo);
				break;
			case CurrOrg:
				outputVo = colltWriteOffAppService.queryAllCurrOrg(vo);
				break;
			case CurrDownOrg:
				outputVo = colltWriteOffAppService.queryAllCurrDownOrg(vo);
				break;
			case CurrOwnerPrd:
				outputVo = colltWriteOffAppService.queryAllCurrOwnerPrd(vo);
				break;
			default:
				outputVo = colltWriteOffAppService.queryAllOwner(vo);
				break;
		}
		return getResponseData(outputVo);
	}

	/**
	 * 查询逾期超过180天的数据
	 * @author xiezw
	 * @params  * @param null
	 * @return
	 * @date 2020/3/10 16:43
	 * @version 0.1
	 */
	@RequestMapping(value="/collt/write/off/app/query",method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ColltTaskInfoVO>> queryColltaskInfo(@RequestBody ColltTaskInfoVO vo){
		List<ColltTaskInfoVO> outputVo = null;
		if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
			logger.info("开始导出数据！");
			outputVo = colltTaskInfoService.queryExInfosOverdue(vo);
		} else {
			logger.info("开始查询数据！");
			outputVo = colltTaskInfoService.queryInfosOverdue(vo);
		}
		addElements(outputVo);
		return getResponseData(outputVo);
	}

	private void addElements(List<ColltTaskInfoVO> outputVo) {
		outputVo.forEach(item -> {
			AccLoanVO accLoanVo = new AccLoanVO();
			accLoanVo.setBillNo(item.getLoanNo());
			AccLoanVO queryRsAccLoanVo = Optional.ofNullable(accLoanService.queryByPk(accLoanVo)).orElse(new AccLoanVO());
			item.setCertType(queryRsAccLoanVo.getCertType());
			item.setCertCode(queryRsAccLoanVo.getCertCode());
			item.setContNo(queryRsAccLoanVo.getContNo());
			item.setChannelCode(queryRsAccLoanVo.getChannelCode());
			item.setPrdName(queryRsAccLoanVo.getPrdName());
			item.setLoanAmount(queryRsAccLoanVo.getLoanAmount());
			item.setLoanStartDate(queryRsAccLoanVo.getLoanStartDate());
			item.setLoanEndDate(queryRsAccLoanVo.getLoanEndDate());
			item.setAccountStatus(queryRsAccLoanVo.getAccountStatus());
		});
	}

}
